let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
    lose_index:0,
    lose_type_index:0,
 
      
    },  

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        var id = options.id;
       // this.getCustomer(id);
        that.setData({
            platform:app.globalData.platform,    
            id:id,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack(){
        console.log('go back')
        wx.navigateBack(-1)
      },
      goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){
    
        }else{
            if(url=='index'){
                wx.reLaunch({
                  url: url,
                })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
        
        }
        console.log(url);
    },
      getLoseSelect(){
        let that = this;
        var open=wx.getStorageSync('open');
        var id = this.data.id
       wx.request({
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
         data: {
           //user_id:open.id,
           //mobile:open.phone,
           id:id
           //phone:open.phone
         
             
         },
         //method: 'POST',
         dataType: 'json',
         timeout: 30000,
         url: app.globalData.domain + 'wechat/customer/getLoseSelect',
         success: (res) => {
              let data = res.data;
           
         
         console.log(data);
           that.setData({
            //follows:follows,
            lose:data.lose,
           // level:levels,
            lose_type:data.lose_type
          
           })
         }
       
        })
      },
      loseChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          lose_index:index
        })
      },
      loseTypeChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          lose_type_index:index
        })
      },
      onSubmit(e){
        wx.showModal({
          title: '',
          content: '您确定要战败么',
          cancelText:'考虑一下',
          confirmText:'确定战败',
          complete: (res) => {
            if (res.cancel) {
                console.log('---cancle--');
                wx.navigateBack();
            }
        
            if (res.confirm) {
                console.log(`数据：${JSON.stringify(e.detail.value)}`)
        let that = this;
        console.log(e);
        var data= this.data;
        var value = e.detail.value;
       // value.inDate = this.data.inDate;
        var open = wx.getStorageSync('open');
        if(value.lose == 0){
          
            //console.log(stock_index);
            this.showModals('请选择本次战败类型','#','#')
            return false;
          }
          if(value.lose_type == 0){
          
            //console.log(stock_index);
            this.showModals('请选择本次战败原因','#','#')
            return false;
          }
          if(value.lose_content.length==0){
            this.showModals('请录入本次跟进内容','#','#')
            return false;
          }
   
          var id = this.data.id;
    
          wx.request({
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: {
              user_id:open.id,
              id:id,             
              lose:value.lose,
              lose_type:value.lose_type,
              lose_content:value.lose_content,
            
            },
            //method: 'POST',
            dataType: 'json',
            timeout: 30000,
            url: app.globalData.domain + 'wechat/customer/toLose',
            success:res=>{
                //console.log(res);
                let data = res.data;
                if(data.status==200){
                    wx.showToast({
                      title: '客户已经战败',
                      duration:1000,
                      success:res=>{
                          wx.navigateBack()
                      }
                    })
                }else{
                    wx.showToast({
                        title: '战败失败',
                        duration:1000,
                        success:res=>{
                            wx.navigateBack()
                        }
                      })
                }
            }
          })
            }
          }
        })
      
       },
    /**
     * 生命周期函数--监听页面显示
     */
    showModals(msg,url,index){
        wx.showModal({
          title: '重要提醒',
          content: msg,
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                if(url){
                    if(index=="index"){
                        wx.reLaunch({
                            url:url
                        })
                    }else{
                        wx.navigateTo({
                          url: url,
                        })
                    }   
                }
            }
          }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getLoseSelect();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})